﻿using System;
using System.Text;
using System.Web.UI.WebControls;
using Controller;

public partial class Modules_NewPostsControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData(0);
        }
    }

    public void BindData(int pageNo)
    {
        var postController = new PostController();
        var data = postController.GetListPost();
        // paging
        var currentPage = pageNo;
        litCurrentPage.Text = currentPage.ToString();
        var nextPage = currentPage + 1;
        litNextpage.Text = nextPage.ToString();
        var prePage = currentPage - 1;
        litPrepage.Text = prePage.ToString();
        var pagedDataSource = new PagedDataSource
            {
                DataSource = data,
                AllowPaging = true,
                PageSize = 5,
                CurrentPageIndex = currentPage
            };

        repeaterPosts.DataSource = pagedDataSource;
        // repeaterPosts.DataSource = data;
        repeaterPosts.DataBind();
    }

    public string SpilitTag(string strTags)
    {
        var stringBuilder = new StringBuilder();
        string[] tags = strTags.Split(',');
        foreach (var tag in tags)
        {
            stringBuilder.Append("<a class=\"badge\" href=\"./label/"+ tag +"\">"+ tag +"</a> ");            
        }        

        return stringBuilder.ToString();
    }

    public string GetNameofUser(int idUser)
    {
        var userController = new UserController();
        var model = userController.GetInfoUser(idUser);
        return model[0].FullName;
    }

    public string GetNameofCategory(string idCategory)
    {
        var categoryController = new CategoryController();
        var model = categoryController.GetInfoOfCategory(idCategory);
        return model[0].Name;
    }

    protected void lbNewer_OnClick(object sender, EventArgs e)
    {
        var prePage = litPrepage.Text;
        BindData(Convert.ToInt32(prePage));
    }

    protected void lbOlder_OnClick(object sender, EventArgs e)
    {
        var nextPage = litNextpage.Text;
        BindData(Convert.ToInt32(nextPage));
    }
}